package com.hycat.mail.product.service.impl;


import com.hycat.mail.product.dao.ProductAttrValueDao;
import com.hycat.mail.product.dao.SkuSaleAttrValueDao;
import com.hycat.mail.product.entity.SkuImagesEntity;
import com.hycat.mail.product.entity.SkuInfoEntity;
import com.hycat.mail.product.entity.SpuInfoDescEntity;
import com.hycat.mail.product.service.*;
import com.hycat.mail.product.vo.ProductItemVo;
import com.hycat.mail.product.vo.SpuBasicAttr;
import com.hycat.mail.product.vo.SpuSaleAttr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @author : lzhycat
 * @date : 2022-08-09 16:31
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private ThreadPoolExecutor executor;


    @Override
    public ProductItemVo productItem(Long skuId) {
        ProductItemVo res = new ProductItemVo();
        //sku基本信息
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = skuInfoService.getBySkuId(skuId);
            res.setSkuInfoEntity(skuInfo);
            return skuInfo;
        }, executor);

        //基本属性
        CompletableFuture<Void> basicAttrFuture = skuInfoFuture.thenAcceptAsync((skuInfo) -> {
            List<SpuBasicAttr> spuBasicAttr = productAttrValueDao.listBasicAttrInfo(skuInfo.getSpuId());
            res.setSpuBasicAttrs(spuBasicAttr);
        }, executor);

        //销售属性
        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync((skuInfo) -> {
            List<SpuSaleAttr> spuSaleAttr = skuSaleAttrValueDao.listSaleAttrInfo(skuInfo.getSpuId());
            res.setSpuSaleAttrs(spuSaleAttr);
        }, executor);

        //sku图片信息
        CompletableFuture<Void> skuImageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> list = skuImagesService.getBySkuId(skuId);
            res.setSkuImages(list);
        }, executor);


        //spu 基本介绍
        CompletableFuture<Void> spuDescFuture = skuInfoFuture.thenAcceptAsync((skuInfo) -> {
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getBySkuId(skuInfo.getSpuId());
            res.setSpuInfoDescEntity(spuInfoDesc);
        }, executor);

        CompletableFuture.allOf(basicAttrFuture, saleAttrFuture, skuImageFuture, spuDescFuture).join();

        return res;
    }

}
